//Axios自定义配置
import axios from 'axios'
import {Message} from 'element-ui'
import store from 'store'

let instance = axios.create({
  baseURL:process.env.VUE_APP_BASE_URL,
  timeout:10000
})

instance.interceptors.request.use((config)=>{
  //去Vuex里获取token，如果有token就把token带上
  let token = store.getters['admin/getToken']
  if(token){
    config.headers['Authorization'] = token;
  }
  return config;
},(error)=>{
  console.log("request error:",error);
  Message('网络繁忙!')
  return Promise.reject(error);
})

instance.interceptors.response.use((config)=>{
  //判断各种业务状态码
  let res = config.data;
  //由于接口状态码结果没有统一，所以兼容了各种写法
  if(res.code==200 || res.status==200 || res.meta?.status==200){
    return Promise.resolve(res);
  }else{
    return Promise.reject(res.msg);
  }
},(error)=>{
  console.log("request error:",error);
  Message('网络繁忙!')
  return Promise.reject(error);
})


export default instance;